package cn.nawang.ebeim.api.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName: HttpTokenUtils
 * @Description: TODO
 * @author fanyb
 * @date Sep 24, 2015 2:01:25 PM
 * @version v1.0
 */
public class HttpTokenUtils {
  // 填充分割符
  private static final String SPIK_TOKEN = "~!@#$%^&*()_,./[]{}|YEFSOFT";

  // 加密DES秘钥
  private static final String DES_KEY = "!@#YEBSOFT$%^&*()_,./[]{}|";

  /**
   * 
   * 
   * @Description: <pre>
   * 创建一个新的访问令牌
   * </pre>
   * @param userID
   * @return
   */
  public static String createHttpToken(String userID) {
    try {

      String grantTime = DateUtils.date2StringYMDHMS();
      String sessionID = UuidUtil.getUuid();
      JSONObject tokenJson = new JSONObject();
      tokenJson.put("userID", userID);
      tokenJson.put("grantTime", grantTime);
      tokenJson.put("sessionID", sessionID);
      tokenJson.put("checksum", getChecksum(userID, grantTime, sessionID));
      String token = tokenJson.toJSONString();
      // 加密

      DesUtil desUtils = new DesUtil();
      desUtils.init(DES_KEY);
      byte[] desBytes = desUtils.desEncrypt(token.getBytes());
      return Base64.encodeBase64String(desBytes);
    } catch (Throwable e) {
      throw new RuntimeException("创建访问令牌失败");
    }
  }

  /**
   * 
   * 
   * @Description: <pre>
   * 解密访问令牌
   * </pre>
   * @param token
   * @return
   */
  public static JSONObject parseHttpToken(String token) {
    try {

      byte[] baseBytes = Base64.decodeBase64(token);
      // 解密
      DesUtil desUtils = new DesUtil();
      desUtils.init(DES_KEY);
      byte[] desBytes = desUtils.desDecrypt(baseBytes);
      return JSON.parseObject(new String(desBytes));
    } catch (Throwable e) {
      throw new RuntimeException("解析访问令牌失败");
    }
  }

  /**
   * 
   * 
   * @Description: <pre>
   * 校验校验码是否合法
   * </pre>
   * @param userID
   * @param grantTime
   * @param sessionID
   * @param checksum
   * @return
   */
  public static boolean checksum(String userID, String grantTime, String sessionID,
      String checksum) {
    // 比对签名
    if (getChecksum(userID, grantTime, sessionID).compareToIgnoreCase(checksum) == 0) {
      // 成功
      return true;
    }
    return false;
  }

  public static boolean checksum(JSONObject tokenJson) {
    String userID = JsonUtil.getJsonString(tokenJson, "userID");
    // 授权时间
    String grantTime = JsonUtil.getJsonString(tokenJson, "grantTime");
    // 会话编号
    String sessionID = JsonUtil.getJsonString(tokenJson, "sessionID");
    // 校验码
    String checksum = JsonUtil.getJsonString(tokenJson, "checksum");
    // 比对签名
    return checksum(userID, grantTime, sessionID, checksum);
  }

  public static boolean checksum(String tokenBase64) {
    JSONObject tokenJson = HttpTokenUtils.parseHttpToken(tokenBase64);
    // 比对签名
    return checksum(tokenJson);
  }

  /**
   * 
   * 
   * @Description: <pre>
   * 获取校验码
   * </pre>
   * @param userID
   * @param grantTime
   * @param sessionID
   * @return
   */
  public static String getChecksum(String userID, String grantTime, String sessionID) {
    StringBuffer temp = new StringBuffer(SPIK_TOKEN);
    temp.append(userID).append(SPIK_TOKEN).append(grantTime).append(SPIK_TOKEN).append(sessionID)
        .append(SPIK_TOKEN);
    return DigestUtils.md5Hex(temp.toString());
  }
}
